//package com.example.java.kafka.es9client;
//
//import co.elastic.clients.elasticsearch._types.SortOptions;
//import co.elastic.clients.elasticsearch._types.SortOrder;
//import co.elastic.clients.elasticsearch._types.aggregations.LongTermsAggregate;
//import co.elastic.clients.elasticsearch._types.aggregations.LongTermsBucket;
//import co.elastic.clients.elasticsearch._types.aggregations.MaxAggregate;
//import co.elastic.clients.elasticsearch._types.query_dsl.Query;
//import co.elastic.clients.elasticsearch.core.SearchResponse;
//import co.elastic.clients.elasticsearch.core.search.HighlightField;
//import co.elastic.clients.util.NamedValue;
//
//import java.util.List;
//
//public class QueryDoc {
//    public static final ElasticsearchTask SEARCH_ALL = client -> {
//        SearchResponse<User> searchResponse = client.search(s -> s
//                        .index("user")
//                        .query(q -> q.matchAll(ma -> ma)),
//                User.class
//        );
//        System.out.println("Result: " + searchResponse);
//    };
//
//    public static final ElasticsearchTask SEARCH_BY_CONDITION = client -> {
//        SearchResponse<User> searchResponse = client.search(s -> s
//                        .index("user")
//                        .query(q -> q.term(t -> t.field("age").value(30))),
//                User.class
//        );
//        System.out.println("Result: " + searchResponse);
//    };
//
//    public static final ElasticsearchTask SEARCH_BY_PAGING = client -> {
//        SearchResponse<User> searchResponse = client.search(s -> s
//                        .index("user")
//                        .query(q -> q.matchAll(ma -> ma))
//                        .from(0)
//                        .size(2),
//                User.class
//        );
//        System.out.println("Result: " + searchResponse);
//    };
//
//    public static final ElasticsearchTask SEARCH_WITH_ORDER = client -> {
//        SearchResponse<User> searchResponse = client.search(s -> s
//                        .index("user")
//                        .query(q -> q.matchAll(ma -> ma))
//                        .sort(SortOptions.of(so -> so.field(f -> f.field("age").order(SortOrder.Asc)))),
//                User.class
//        );
//        System.out.println("Result: " + searchResponse);
//    };
//
//    public static final ElasticsearchTask SEARCH_BY_BOOL_CONDITION = client -> {
//        SearchResponse<User> searchResponse = client.search(s -> s
//                        .index("user")
//                        .query(q -> q.bool(b -> b
//                                .must(m -> m.match(t -> t.field("age").query("30")))
//                                .mustNot(m -> m.match(t -> t.field("name").query("zhangsan")))
//                                .should(m -> m.match(t -> t.field("sex").query("男")))
//                        )),
//                User.class
//        );
//        System.out.println("Result: " + searchResponse);
//    };
//
//    public static final ElasticsearchTask SEARCH_BY_RANGE = client -> {
//        Query query = Query.of(q -> q.range(r -> r
//                .number(nb -> nb
//                        .field("age")
//                        .lte(Double.valueOf("40"))
//                        .gte(Double.valueOf("18"))
//                )
//        ));
//        SearchResponse<User> response = client.search(
//                s -> s.index("user").query(query),
//                User.class
//        );
//        System.out.println("命中数: " + response.hits().hits().size());
//    };
//
//    public static final ElasticsearchTask SEARCH_BY_FUZZY_CONDITION = client -> {
//        SearchResponse<User> searchResponse = client.search(s -> s
//                        .index("user")
//                        .query(q -> q.fuzzy(f -> f
//                                .field("name")
//                                .value("wangwu")
//                                .fuzziness("1")
//                        )),
//                User.class
//        );
//        System.out.println("Result: " + searchResponse);
//    };
//
//    public static final ElasticsearchTask SEARCH_WITH_HIGHLIGHT = client -> {
//        SearchResponse<User> searchResponse = client.search(s -> s
//                        .index("user")
//                        .query(q -> q.match(m -> m.field("name").query("zhangsan")))
//                        .highlight(h -> h.fields(
//                                        NamedValue.of("name", HighlightField.of(hf -> hf
//                                                .preTags("<font color='red'>")
//                                                .postTags("</font>")
//                                        ))
//                                )
//                        ),
//                User.class
//        );
//        System.out.println("hits========>>");
//        searchResponse.hits().hits().forEach(hit -> {
//            System.out.println("source: " + hit.source());
//            if (hit.highlight() != null) {
//                System.out.println("highlight: " + hit.highlight());
//            }
//        });
//        System.out.println("<<========");
//    };
//
//    public static final ElasticsearchTask SEARCH_WITH_MAX = client -> {
//        SearchResponse<User> searchResponse = client.search(s -> s
//                        .index("user")
//                        .aggregations("max_age", a -> a.max(m -> m.field("age"))),
//                User.class
//        );
//        MaxAggregate maxAge = searchResponse.aggregations().get("max_age").max();
//        System.out.println("最大年龄: " + maxAge.value());
//    };
//
//    public static final ElasticsearchTask SEARCH_WITH_GROUP = client -> {
//        SearchResponse<User> searchResponse = client.search(s -> s
//                        .index("user")
//                        .aggregations("age_group", a -> a
//                                .terms(t -> t.field("age")) // age 是数值字段
//                        ),
//                User.class
//        );
//        // 正确方式：使用 lterms() 获取 long/integer 字段的聚合结果
//        LongTermsAggregate longTerms = searchResponse.aggregations().get("age_group").lterms();
//        List<LongTermsBucket> buckets = longTerms.buckets().array();
//        for (LongTermsBucket bucket : buckets) {
//            System.out.println("年龄: " + bucket.key() + ", 数量: " + bucket.docCount());
//        }
//    };
//
//    public static void main(String[] args) {
////        ConnectElasticsearch.connect(SEARCH_ALL);
////        ConnectElasticsearch.connect(SEARCH_BY_CONDITION);
////        ConnectElasticsearch.connect(SEARCH_BY_PAGING);
////        ConnectElasticsearch.connect(SEARCH_WITH_ORDER);
////        ConnectElasticsearch.connect(SEARCH_BY_BOOL_CONDITION);
////        ConnectElasticsearch.connect(SEARCH_BY_FUZZY_CONDITION);
////        ConnectElasticsearch.connect(SEARCH_WITH_MAX);
////        ConnectElasticsearch.connect(SEARCH_WITH_GROUP);
////        ConnectElasticsearch.connect(SEARCH_WITH_HIGHLIGHT);
//        ConnectElasticsearch.connect(SEARCH_BY_RANGE);
//    }
//}